############################################################
# auther	: sgxiao
# Date		: 2009-01-07
# version	: 0.2.3
# history
#	sgxiao	0.2.3	modify the filename
#	sgxiao	0.2.2	debug the formulate of caculating return
#	sgxiao	0.2.1	add the right_price to the Column B
#	sgxiao	0.1	implements a format of month.return
#############################################################
	



#arguments list
# 1.worksheet	2.average_days 1 2 5 10 50 100 150 200	3. $jj	4. left_num	5.right_num	6.sheet1	7.state1
sub caculate_return {
my ($worksheet, $num, $kk, $left_num, $right_num, $last, ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j), $last_right_price_buy, $right_price_buy) = @_;
	my $current;
	my $close_price_return="";
	my $right_price_return="";
	
	my $jj2 = $kk + 1;
	if($kk >= ($num - 1)) {

		$right_price_buy = $j;

		if( $last == 0 ) {
			if($left_num > $right_num) {
				$current = 1;
			} else {
				$current = 0;
			}	
		} else {
			if($left_num > $right_num) {
				$current = 1;
			} else {
				$current=0;				
			}

			$right_price_return = $j / $last_right_price_buy  - 1;
		}
		$last = $current;
	}
	system "echo $a\\\t$j\\\t$left_num\\\t$right_num\\\t\"=IF(C\$jj2>D\$jj2, \"True\", \"False\")\"\\\t$right_price_return >> $worksheet";
	$last_right_price_buy = $right_price_buy;
	($last, $last_right_price_buy, $right_price_buy);
}






####  main()
$worksheet1 = "${filename}.1_50.month.return";
$worksheet2 = "${filename}.1_150.month.return";
$worksheet3 = "${filename}.1_200.month.return";
$worksheet4 = "${filename}.2_200.month.return";
$worksheet5 = "${filename}.5_150.month.return";
$worksheet6 = "${filename}.10_200.month.return";

my $last1=0;
my $last_right_price_buy1_50 = 0;
my $right_price_buy1_50;
my $close_price_return1_50;
my $right_price_return1_50;

my $last2=0;
my $last_right_price_buy1_150 = 0;
my $right_price_buy1_150;
my $close_price_return1_150;
my $right_price_return1_150;

my $last3=0;
my $last_right_price_buy1_200 = 0;
my $right_price_buy1_200;
my $close_price_return1_200;
my $right_price_return1_200;

my $last4=0;
my $last_right_price_buy2_200 = 0;
my $right_price_buy2_200;
my $close_price_return2_200;
my $right_price_return2_200;

my $last5=0;
my $last_right_price_buy5_150 = 0;
my $right_price_buy5_150;
my $close_price_return5_150;
my $right_price_return5_150;

my $last6=0;
my $last_right_price_buy10_200 = 0;
my $right_price_buy10_200;
my $close_price_return10_200;
my $right_price_return10_200;


$jj=0;




while(<CONFIG>) {
	chomp;
	@array = split /\t/, $_;

	#	a 	b 	c 	d 	e 	f	 g 	h 	i 	j
	#	d 	1 	2 	5 	10 	50 	100	150	200	r
	
	my($a, $b, $c, $d, $e, $f, $g, $h, $i, $j) = @array;
	# 1.worksheet	2.average_days 1 2 5 10 50 100 150 200		4. left_num	5.right_num	6.sheet1	7.state1
	
	($last1, $last_right_price_buy1_50, $right_price_buy1_50) = &caculate_return($worksheet1, 50, $jj, $j, $f, $last1, @array, $last_right_price_buy1_50, $right_price_buy1_50);
	($last2, $last_right_price_buy1_150, $right_price_buy1_150) = &caculate_return($worksheet2, 150, $jj, $j, $h, $last2, @array, $last_right_price_buy1_150, $right_price_buy1_150);
	($last3, $last_right_price_buy1_200, $right_price_buy1_200) = &caculate_return($worksheet3, 200, $jj, $j, $i, $last3, @array, $last_right_price_buy1_200, $right_price_buy1_200);
	($last4, $last_right_price_buy2_200, $right_price_buy2_200) = &caculate_return($worksheet4, 200, $jj, $c, $i, $last4, @array, $last_right_price_buy2_200, $right_price_buy2_200);
	($last5, $last_right_price_buy5_150, $right_price_buy5_150) = &caculate_return($worksheet5, 150, $jj, $d, $h, $last5, @array, $last_right_price_buy5_150, $right_price_buy5_150);
	($last6, $last_right_price_buy10_200, $right_price_buy10_200) = &caculate_return($worksheet6, 200, $jj, $e, $i, $last6, @array, $last_right_price_buy10_200, $right_price_buy10_200);
	if( $jj % 100 eq 0 ) {
		printf("%d\n", $jj);
	}
	$jj++;
}